package cn.yhbk.web.modules.manager.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResult;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResultItem;
import cn.yhbk.web.modules.manager.Mapper.OrderResultItemMapper;
import cn.yhbk.web.modules.manager.Pojo.VO.OrderResult.OrderResultClassIdRes;
import cn.yhbk.web.modules.manager.Service.IOrderResultItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2024-02-20
 */
@Service
public class OrderResultItemServiceImpl extends ServiceImpl<OrderResultItemMapper, OrderResultItem> implements IOrderResultItemService {

    @Override
    public List<OrderResultClassIdRes> packageResult(List<OrderResult> records) {
        //去重和过滤非空，拿到集合id
        List<Long> resultIds = records.stream().map(OrderResult::getId)
                .filter(item -> !Objects.isNull(item))
                .distinct()
                .collect(Collectors.toList());
        System.out.println(resultIds);


        if(resultIds.isEmpty())return new ArrayList<>();
        LambdaQueryWrapper<OrderResultItem> lambda = new QueryWrapper<OrderResultItem>().lambda();

        lambda.in(OrderResultItem::getOrderResultId,resultIds);

        //查询拿到每一个具体项
        List<OrderResultItem> list = list(lambda);

        //分局集合id分类，拿到每个集合的班级id
        Map<Long, List<OrderResultItem>> classIdMap = list.stream().collect(Collectors.groupingBy(OrderResultItem::getOrderResultId));

        return records.stream().map(item->{
            OrderResultClassIdRes resTmp = new OrderResultClassIdRes();
            BeanUtil.copyProperties(item,resTmp);
            List<OrderResultItem> orderResultItems = classIdMap.get(item.getId());
            if(Objects.isNull(orderResultItems)){
                resTmp.setClassIdList(new ArrayList<>());
            }else{
                resTmp.setClassIdList(orderResultItems);
            }
            return resTmp;
        }).collect(Collectors.toList());

    }

    @Override
    public List<Long> packageResultByResultId(Long resultId) {
        if(Objects.isNull(resultId))return new ArrayList<>();
        LambdaQueryWrapper<OrderResultItem> lambda = new QueryWrapper<OrderResultItem>().lambda();

        lambda.eq(OrderResultItem::getOrderResultId,resultId);
        List<OrderResultItem> list = list(lambda);
        return list.stream().map(OrderResultItem::getClassId).collect(Collectors.toList());
    }

    @Override
    public void removeByResultId(Long orderResultId) {
        LambdaQueryWrapper<OrderResultItem> lambda = new QueryWrapper<OrderResultItem>().lambda();

        lambda.eq(OrderResultItem::getOrderResultId,orderResultId);

        remove(lambda);
    }
}
